<?php

/**
 * @see http://www.mahuohuo.com/
 * @copyright mahuohuo.com
 * @author mahuohuo
 * @link http://www.mahuohuo.com
 * 
 * @example
 * 模型
 * 固定的前缀KEY   value = 序列化的多维数组
 * 3
 */
class SaeTaskQueue {
    private $queuename = '';
    private $errno = 0;
    private $queuallcontent = array();
    private static $redis = null;
    const queueprefix = 'queuefilename';
    public function __construct ($queue_name){
        $this->queuename = constant(self::queueprefix).$queue_name;
        
        if(!(self::$redis instanceof Redis)){ // && is_callable(self::$redis)
            self::$redis = new Redis();
            self::$redis->select(3);
        }
        self::$redis->connect('127.0.0.1',6379);
        
        if( count( (self::$redis->getMultiple(array(constant(self::queueprefix).'*')) ) ) > 10 ){
            $this->errno = 11;
        }
        
        //$this->__autoload();
    }
//    public function __autoload(){
//     }
    
    public function __destruct() {
//        var_dump($this->queuallcontent);
        return self::$redis->set($this->queuename,  serialize($this->queuallcontent));
    }
    public function push(){
        return $this->__destruct();
    }

    public function curLength (){//这个写法又奔放又惊悚
        return (count((array)self::$redis->get($this->queuename)));
    }
    public function leftLength (){
        return 10;
    }
    
    public function addTask($tasks,$postdata = NULL, $prior = false, $options = array('delay'=>0)){
        //echo __FUNCTION__.'<br>';
        //var_dump($this->queuallcontent);
        $adddata = array(
                'url' => '',
              'postdata' => base64_encode($postdata), //要POST的数据。可选
              'prior' => $prior,  //是否优先执行，默认为false，如果设为true，则将此任务插入到队列最前面。可选
              'options' => $options);
        
        if(is_string($tasks)){
            if(!filter_var($tasks,FILTER_VALIDATE_URL)){
                $this->errno = '3';
                return FALSE;
            }
            $adddata['url'] = urlencode($tasks);
            
            if($adddata['prior']){//优先级
                array_unshift($this->queuallcontent, $adddata);
            }else{
                array_push($this->queuallcontent, $adddata);
            }
        }  elseif (is_array($tasks)) {
            //$tasks = array_values($tasks);
           //http://apidoc.sinaapp.com/__filesource/fsource_sae__saetaskqueue.class.php.html#a100
           //写得那叫一个复杂啊。。。
            foreach ($tasks as $key) {
//                if(count(array_diff_key ($key,$adddata)) > 0){//判断KEY 不知道为什么随机性的不对。
//                    $this->errno = '3';
//                    return FALSE;
//                }
                call_user_func_array(array(__CLASS__,__FUNCTION__),$key);
            }
        }
        return TRUE;
    }
    
    public function errmsg (){
         $msg = array('<a href="https://gitee.com/foglede" onMouseOver="javascript:void(0)">mahuohuo</a> 为您的本地SAE仿真环境，不会出错！',
             '认证失败',
             '3' => '参数错误',
             '10'  => '队列不存在',
             '11'  => '<a href="https://gitee.com/foglede" onMouseOver="javascript:void()">队列已满或剩余长度不足</a>',
             '403' => '权限不足或超出配额',
             '500' => '服务内部错误',
             '999' => '未知错误'
             );
         return $msg[$this->errno];
     }
     
     public function errno () {
         return $this->errno;
     }
     
     public function setAuth(){}
}

?>
